package com.csjbot.snowbotagingtest;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.csjbot.snowbot_rogue.utils.CSJToast;
import com.csjbot.snowbotagingtest.base.AIUIActivity;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by jingwc on 2017/7/29.
 */

public class TestInfoActivity extends AIUIActivity {

    @BindView(R.id.tv_tag)
    TextView tv_tag;
    @BindView(R.id.tv_partol_error_count)
    TextView tv_partol_error_count;
    @BindView(R.id.tv_camera)
    TextView tv_carmera;
    @BindView(R.id.tv_left_arm)
    TextView tv_left_arm;
    @BindView(R.id.tv_right_arm)
    TextView tv_right_arm;
    @BindView(R.id.tv_double_arm)
    TextView tv_double_arm;
    @BindView(R.id.tv_expression)
    TextView tv_expression;
    @BindView(R.id.tv_speak)
    TextView tv_speak;
    @BindView(R.id.tv_aiui_status)
    TextView tv_aiui_status;
    @BindView(R.id.tv_speeding)
    TextView tv_speeding;

    private TestBean mTestBean;

    @Override
    public void afterViewCreated(Bundle savedInstanceState) {
        showTestInfo();
    }

    /**
     * 显示测试结果
     */
    private void showTestInfo() {
        mTestBean = (TestBean) getIntent().getExtras().getSerializable("testinfo");
        if(mTestBean != null){
            tv_tag.setText("测试Tag:"+mTestBean.getTag());
            tv_partol_error_count.setText("巡逻点失败次数:"+mTestBean.getPartolErrorCount());
            tv_carmera.setText("摄像头打开状态:"+(mTestBean.isCameraOpenStatus()?"成功":"失败"));
            tv_left_arm.setText("左臂摆动次数:"+mTestBean.getLeftArmCount());
            tv_right_arm.setText("右臂摆动次数:"+mTestBean.getRightArmCount());
            tv_double_arm.setText("双臂摆动次数:"+mTestBean.getDoubleArmCount());
            tv_speak.setText("说话次数:"+mTestBean.getSpeakCount());
            tv_expression.setText("变换表情次数:"+mTestBean.getExpressionCount());
            tv_aiui_status.setText("AIUI连接状态:"+(mTestBean.isAiuiStatus()?"成功":"失败"));
            tv_speeding.setText("超速状态:"+(mTestBean.isSpeeding()?"出现":"未出现"));
        }
    }

    @OnClick(R.id.bt_save)
    public void save(){
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                saveBitmap();
//            }
//        }).start();
        saveBitmap();
    }

    @OnClick(R.id.bt_exit)
    public void exit(){
        RogueUtils.kill(getPackageName());
    }

    /**
     * 将View转换为bitmap
     * @return
     */
    private Bitmap createBitmap(){
        View view = getWindow().getDecorView();
        Bitmap bm = Bitmap.createBitmap(view.getMeasuredWidth(),view.getMeasuredHeight(),Bitmap.Config.RGB_565);
        view.draw(new Canvas(bm));
        return bm;
    }

    /**
     * 保存bitmap到本地
     */
    public void saveBitmap() {
        Bitmap bm = createBitmap();
        if(bm == null){
            return;
        }
        if(TextUtils.isEmpty(mTestBean.getTag())){
            return;
        }
        File testDir = new File(ConstantsPath.testResultPath);
        if(!testDir.exists()){
            testDir.mkdirs();
        }
        File testImageDir = new File(ConstantsPath.testImageResultPath);
        if(!testImageDir.exists()){
            testImageDir.mkdir();
        }
        File f = new File(testImageDir,mTestBean.getTag());
        try {
            f.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
            CSJToast.showToast(TestInfoActivity.this,"保存成功");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    public void setListener() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.actvity_testinfo;
    }
}
